package com.eian.boot.common.core.model;

import lombok.Data;

/**
 * 响应信息主体
 *
 * @author alex.meng
 */
@Data
public class CommonResult<T> {
    /**
     * 成功
     */
    public static final int SUCCESS = 200;
    public static final String SUCCESS_MSG = "操作成功";
    /**
     * 失败
     */
    public static final int FAIL = 500;
    public static final String FAIL_MSG = "操作失败";

    private int code;
    private String msg;
    private T data;

    private static <T> CommonResult<T> restResult(T data, int code, String msg) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setData(data);
        commonResult.setMsg(msg);
        return commonResult;
    }

    public static <T> CommonResult<T> ok() {
        return restResult(null, SUCCESS, SUCCESS_MSG);
    }

    public static <T> CommonResult<T> ok(T data) {
        return restResult(data, SUCCESS, SUCCESS_MSG);
    }

    public static <T> CommonResult<T> ok(String msg) {
        return restResult(null, SUCCESS, msg);
    }

    public static <T> CommonResult<T> okStr(T data) {
        return restResult(data, SUCCESS, SUCCESS_MSG);
    }

    public static <T> CommonResult<T> ok(String msg, T data) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> CommonResult<T> fail() {
        return restResult(null, FAIL, FAIL_MSG);
    }

    public static <T> CommonResult<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> CommonResult<T> fail(String msg, T data) {
        return restResult(data, FAIL, msg);
    }

    public static <T> CommonResult<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

}